﻿using GZNest.Biz.IService;
using GZNest.Biz.Service.Spin.System;
using GZNest.Entity.Spin.System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace GZNest.Web.Areas.Spin.Controllers
{
    public class ProductController : Controller
    {
        private IProductService productService;
        public ProductController()
        {
            productService = new ProductService();
        }

        // GET: Spin/Product
        public ActionResult Index()
        {
            var p = productService.FindAll();
            return View(p);
            
        }

        // GET: Spin/Product/Details/5
        public ActionResult Details(int id)
        {
            Product m = productService.Find(id);
            if (m == null)
            {
                return HttpNotFound();
            }
            return View(m);
        }

        // GET: Spin/Product/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: Spin/Product/Create
        [HttpPost]
        public ActionResult Create(Product newProduct)
        {
            try
            {
                // TODO: Add insert logic here
                if (ModelState.IsValid)
                {
                    productService.Add(newProduct);
                    return RedirectToAction("Index");
                }
                else
                {
                    return View(newProduct);
                }
            }
            catch
            {
                return View();
            }
        }

        // GET: Spin/Product/Edit/5
        public ActionResult Edit(int id)
        {
            Product m = productService.Find(id);
            if (m == null)
            {
                return HttpNotFound();
            }
            return View(m);
        }

        // POST: Spin/Product/Edit/5
        [HttpPost]
        public ActionResult Edit(Product product)
        {
            try
            {
                // TODO: Add update logic here
                if (ModelState.IsValid)
                {
                    productService.Update(product);
                    return RedirectToAction("Index");
                }
                else
                {
                    return View(product);
                }
            }
            catch
            {
                return View();
            }
        }

        // GET: Spin/Product/Delete/5
        public ActionResult Delete(int id)
        {
            return View();
        }

        // POST: Spin/Product/Delete/5
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
